package com.cyf.activemq.service.impl;

import com.cyf.activemq.network.bean.*;
import com.cyf.activemq.network.bean.person.MqGetEventRecordRsp;
import com.cyf.activemq.network.bean.person.MqSendStatisticalDataReq;
import com.cyf.activemq.network.bean.third.kuting.MqKTCreOrderNoticeRsp;
import com.cyf.activemq.network.bean.third.kuting.MqKTDelOrderNoticeRsp;
import com.cyf.activemq.service.MqService;
import com.cyf.activemq.service.ReceiveMsgService;
import com.cyf.activemq.service.bean.QueueList;
import com.cyf.redis.RedisService;
import com.cyf.util.BasicProperties;
import com.cyf.util.ObjectMapperUtils;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 接收消息
 * Created by cage on 2017/3/15.
 */
@Service
public class ReceiveMsgServiceImpl implements ReceiveMsgService {

    private static final Logger logger = LoggerFactory.getLogger(ReceiveMsgServiceImpl.class);
    @Autowired
    private BasicProperties basicProperties;
    @Autowired
    private RedisService redisService;
    @Autowired
    private MqService mqService;

    @JmsListener(destination = QueueList.TOPIC_RECEIVE, containerFactory = "jmsListenerContainerTopic")
    @Override
    public void getQueueMessageText(String message) {
        try {
            if (message == null) {
                return;
            }
            if (message.length() < 400) {
                logger.info("activemq收到消息：{}", message);
            }

            MqBusinessRsp mqBusinessRsp = ObjectMapperUtils.getObjectMapper().readValue(message, MqBusinessRsp.class);
            switch (mqBusinessRsp.getOptid()) {
                // 获取费用
                case MqBusinessOpt.GETFEE:
                    MqGetFeeRsp mqGetFeeRsp = ObjectMapperUtils.getObjectMapper().readValue(message, MqGetFeeRsp.class);
                    redisService.set(mqGetFeeRsp.getUuid(), mqGetFeeRsp, basicProperties.getMqCacheTime());
                    break;
                // 获取费率
                case MqBusinessOpt.GETFEERATE:
                    MqGetFeeRateRsp mqGetFeeRateRsp = ObjectMapperUtils.getObjectMapper().readValue(message, MqGetFeeRateRsp.class);
                    redisService.set(mqGetFeeRateRsp.getUuid(), mqGetFeeRateRsp, basicProperties.getMqCacheTime());
                    break;
                /*// 支付费用
                case MqBusinessOpt.PAYFEE:
                    MqPayFeeRsp mqPayFeeRsp = ObjectMapperUtils.getObjectMapper().readValue(message, MqPayFeeRsp.class);
                    redisService.set(mqPayFeeRsp.getUuid(), mqPayFeeRsp, basicProperties.getMqCacheTime());
                    break;*/
                //锁定解锁车辆
                case MqBusinessOpt.CARLOCKORNO:
                    MqCarLockOrNoRsp mqCarLockOrNoRsp = ObjectMapperUtils.getObjectMapper().readValue(message, MqCarLockOrNoRsp.class);
                    redisService.set(mqCarLockOrNoRsp.getUuid(), mqCarLockOrNoRsp, basicProperties.getMqCacheTime());
                    break;
               /* //月卡充值
                case MqBusinessOpt.CARRECHARGE:
                    MqCarRechargeRsp mqCarRechargeRsp = ObjectMapperUtils.getObjectMapper().readValue(message, MqCarRechargeRsp.class);
                    redisService.set(mqCarRechargeRsp.getUuid(), mqCarRechargeRsp, basicProperties.getMqCacheTime());
                    break;*/
                // 车辆入场通知
                case MqBusinessOpt.CARIN:
                    MqCarInReq mqCarInReq = ObjectMapperUtils.getObjectMapper().readValue(message, MqCarInReq.class);
                    mqService.sendCarInInfoForPmsvr(mqCarInReq);
                    break;
                // 车辆出场通知
                case MqBusinessOpt.CAROUT:
                    MqCarOutReq mqCarOutReq = ObjectMapperUtils.getObjectMapper().readValue(message, MqCarOutReq.class);
                    mqService.sendCarOutInfoForPmsvr(mqCarOutReq);
                    break;
                // 获取入场车辆计费信息描述
                case MqBusinessOpt.GETFEEDESC:
                    MqGetFeeDescRsp mqGetFeeDescRsp = ObjectMapperUtils.getObjectMapper().readValue(message, MqGetFeeDescRsp.class);
                    redisService.set(mqGetFeeDescRsp.getUuid(), mqGetFeeDescRsp, basicProperties.getMqCacheTime());
                    break;
                // 月/年卡车过期通知
                case MqBusinessOpt.EXPIREDWARNING:
                    MqExpiredWarningReq mqExpiredWarningReq = ObjectMapperUtils.getObjectMapper().readValue(message, MqExpiredWarningReq.class);
                    mqService.sendExpiredWarningMsg(mqExpiredWarningReq);
                    break;
                case MqBusinessOpt.TOC_REG_PERSON_AND_CAR:
                    MqRegPersonAndCarRsp mqRegPersonAndCarRsp = ObjectMapperUtils.getObjectMapper().readValue(message, MqRegPersonAndCarRsp.class);
                    redisService.set(mqRegPersonAndCarRsp.getUuid(), mqRegPersonAndCarRsp, basicProperties.getMqCacheTime());
                    break;
                case MqBusinessOpt.TOC_UPDATE_PERSON_AND_CAR:
                    MqUpdatePersonAndCarRsp mqUpdatePersonAndCarRsp = ObjectMapperUtils.getObjectMapper().readValue(message, MqUpdatePersonAndCarRsp.class);
                    redisService.set(mqUpdatePersonAndCarRsp.getUuid(), mqUpdatePersonAndCarRsp, basicProperties.getMqCacheTime());
                    break;

                // 第三方：酷停车位预定通知
                case MqBusinessOpt.KT_CREORDERNOTICE:
                    MqKTCreOrderNoticeRsp mqKTCreOrderNoticeRsp = ObjectMapperUtils.getObjectMapper().readValue(message, MqKTCreOrderNoticeRsp.class);
                    redisService.set(mqKTCreOrderNoticeRsp.getUuid(), mqKTCreOrderNoticeRsp, basicProperties.getMqCacheTime());
                    break;
                // 第三方：酷停取消车位预定通知
                case MqBusinessOpt.KT_DELORDERNOTICE:
                    MqKTDelOrderNoticeRsp mqKTDelOrderNoticeRsp = ObjectMapperUtils.getObjectMapper().readValue(message, MqKTDelOrderNoticeRsp.class);
                    redisService.set(mqKTDelOrderNoticeRsp.getUuid(), mqKTDelOrderNoticeRsp, basicProperties.getMqCacheTime());
                    break;

                // 人管平台：获取部门人员的出入记录
                case MqBusinessOpt.PERSON_GET_EVENTRECORD:
                    MqGetEventRecordRsp mqGetEventRecordRsp = ObjectMapperUtils.getObjectMapper().readValue(message, MqGetEventRecordRsp.class);
                    redisService.set(mqGetEventRecordRsp.getUuid(), mqGetEventRecordRsp, basicProperties.getMqCacheTime());
                    break;
                // 人管平台：部门的统计信息（人管平台主动发送过来）
                case MqBusinessOpt.PERSON_SEND_STATISTIC_DATA:
                    MqSendStatisticalDataReq mqSendStatisticalDataReq = ObjectMapperUtils.getObjectMapper().readValue(message, MqSendStatisticalDataReq.class);
                    mqService.sendStatisticalDataForPerson(mqSendStatisticalDataReq);
                    break;
                default: {
                    logger.warn("activemq消息队列收到未知的请求：optid：{}", mqBusinessRsp.getOptid());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
